--[[
游戏规则
1. 跑胡子，小写“一”到“十”各4张共40张，大写“壹”到“拾”各4张共40张。
2. 砌牌：跑胡子为3人同玩，庄家砌21张，其他方位砌20张，留19张在墩上。庄家砌的最后一张牌必须示众。泸州地区一般四人同玩，也可三人。二人。四人玩时有一人坐底，坐庄家对面，为三玩家数底牌。庄家不示最后一张，但要把底牌最后一张明示。且不能胡最后一张，如遇红字则更换，红字更换到最上方。
3. 一对牌：砌牌后，手中2个相同的牌为1对。
4. 一坎牌：砌牌后，手中3个相同的牌为1坎。一坎牌不能拆散与其他牌组合。
5. 一提牌：砌牌后，手中4个相同的牌为1提。一提牌不能拆散与其他牌组合。在进第一张牌之前，必须放到桌面示众。泸州地区叫一垅。
6. 一句话：砌牌后，手中的牌依据规则组合成相连的三张，比如小四、五、六，称为一句话。另外二、七、十组合也称为一句话。泸州地区叫一列
7. 绞牌：当1对大牌与1张相同的小牌，或者1对小牌与1张相同的大牌组合时，成为绞牌。如1对小九与1张大玖。
]]

local random = math.random
local table_copy = table.copy
local floor = math.floor
local modf = math.modf
local log = require("Log")
local tbl_huxi = require "TblHuxi"
local tbl_split = require "TblSplit"
local Rule = class("Rule")

function  Rule:ctor()
	--随机种子
	math.randomseed(os.time())
	--40张原牌
	self.card = {
		
	}	
	--要发的牌
	self.tbCard = {}
	--牌色
	self.colorType = {
		black=1,
		red=2,
	}
	--牌型
	self.cardType={
		danZhang=1, --单张
		duiZi=2,--一对
		kan=3,--一坎
		ti=4,--一提
		shun=5,--顺子
		jiao=6,-- 绞牌			
	}
	-- 默认可以胡牌的胡息
	self.minHuxi = 15	
	self.tbMingTang = {
		"自摸","一点红","一块扁","海底胡","乌胡",
		"红胡","天胡","地胡","放炮",
	} 	
end

--洗牌
function Rule:shuffle()	
	self.tbCard = {}
	self.card = {}
	--小张		
	for i=1,10 do
		for k=1,4 do
			table.insert(self.card,0x10+i)			
		end
	end
	--大张
	for i=1,10 do
		for k=1,4 do
			table.insert(self.card,0x20+i)			
		end
	end	
	for _, v in pairs(self.card) do 
		table.insert(self.tbCard, v)
	end	
	--搞乱顺序
	table.mix(self.tbCard)
	
  --   local tb  = {
  --     37,38,23,38,19,20,42,18,22,33,33,21,26,22,41,34,24,21,42,34,42,
  --   }
  --   for k,v in pairs(tb) do 
  --   	self.tbCard[k]=v
 	-- end
end

--取指定个数的牌
function Rule:getCard(num)
	local tbRes = {}
	local n = num or 0
	for k,v in pairs(self.tbCard) do 
		if n <= 0 then break end
		if v then 
			n = n-1
			table.insert(tbRes,v)
			self.tbCard[k] = nil
		end
	end
	return tbRes
end
function Rule:setCard(cards1,cards2,cards3)
	-- print(cards1,cards2,cards3)
	print("_____________setCard____")
	local index=0
	for i=1,#cards1 do 
		self.tbCard[i] = cards1[i]
		index = index + 1
	end
	local j = index
	for i=1,#cards2 do 
		self.tbCard[i+j] = cards2[i]
		index = index + 1
	end	
	local j = index
	for i=1,#cards3 do 
		self.tbCard[i+j] = cards3[i]
		index = index + 1
	end		
	-- print("____self.tbCard",self.tbCard)
end

function Rule:getLessCard()
	local tb = {}
	for k,v in pairs(self.tbCard) do 		
		if v then 			
			table.insert(tb,v)
		end
	end
	return tb
end

--取牌值
function Rule:getCardNum(card)
	return card & 0x0f
end

--取牌色
function Rule:getCardColor(card)
	local num = card & 0x0f
	if num == 2 or num == 7 or num == 10 then 
		return self.colorType.red 
	end
	return self.colorType.black
end

--取牌类型
function Rule:getCardType(card)	
	return card & 0xf0
end

function Rule:has(tb,value)
	for i,v in ipairs(tb) do
		if v == value then
			return true
		end
	end
	return false
end

--打印16进制牌
function Rule:printCard(card)
	local str = string.format("0x%x",card)
	print(str)
end

--对牌排序
function Rule:sortCards(tbCard)
	local n1,n2
	local c1,c2
	local function sortFun(a,b)
		n1 = self:getCardNum(a)
		n2 = self:getCardNum(b)
		c1 = self:getCardType(a)
		c2 = self:getCardType(b)		
		if c1 > c2 then 
			return true
		elseif c1 == c2 then 
			if n1 < n2 then 
				return true
			end
		end
		return false
	end
	table.sort(tbCard,sortFun)	
end

--绞牌
function Rule:isJiao(cards)
	if #cards == 3 then
		self:sortCards(cards)
		local num1 = self:getCardNum(cards[1])
		local num2 = self:getCardNum(cards[2])
		local num3 = self:getCardNum(cards[3])
		if self:getCardType(cards[1]) ~= self:getCardType(cards[3]) then
			if num1==num2 and num2==num3 then 
				return true
			end
		end
	end
	return false
end

--一对
function Rule:isDuizi(cards)
	if #cards == 2 then 
		if cards[1] == cards[2] then 
			return true
		end
	end
	return false
end

--一句话
function Rule:isShun(cards)
	if #cards == 3 then
		self:sortCards(cards)
		if cards[1] + 1 == cards[2] and cards[1] + 2 == cards[3] then
			return true
		end
		--2,7,10
		if self:getCardNum(cards[1]) == 2 and cards[1] + 5 == cards[2] 
			and cards[1] + 8 == cards[3] then
			return true
		end
	end
	return false
end

--一坎牌
function Rule:isShun(cards)
	if #cards == 3 then
		self:sortCards(cards)
		if cards[1] == cards[2] and cards[1] == cards[3] then
			return true
		end
	end
	return false
end

--一堤牌
function Rule:isTi(cards)
	if #cards == 4 then
		self:sortCards(cards)
		if cards[1] == cards[4] then
			return true
		end
	end
end

--一手牌中各个牌的个数
function Rule:getAllValueNums(cards)
	local ret = {}
	for _,v in pairs(cards) do
		if ret[v] then
			ret[v] = ret[v] + 1
		else
			ret[v] = 1
		end
	end
	return ret
end
function Rule:get_all_value_nums(cards)
	local ret = {}
	for _,v in pairs(cards) do
		if ret[v] then
			ret[v] = ret[v] + 1
		else
			ret[v] = 1
		end
	end
	return ret
end

--大牌对应的小牌， 小牌对应的大牌
function Rule:getOtherTypeCard(card)
	local cardType = self:getCardType(card)
	if cardType == 0x10 then 
		return card + 0x10 
	elseif cardType == 0x20 then 
		return card - 0x10
	end
	return nil
end

--能否碰
function Rule:checkPeng(cards,card)
	local cardNums = self:getAllValueNums(cards)
	if cardNums[card] == 2 then
		return true
	end
	return false
end

--能否吃
function Rule:checkChi(set,single)
	local _type  = self:getCardType(single)
	local _value = self:getCardNum(single)
	local card_nums = self:getAllValueNums(set)
	--组成顺
	local set_t = {}
	if single <= 0x20 then
		for i=0x10 + 1, 0x10 + 10 do
			set_t[i] = card_nums[i]
		end
	else
		for i=0x20 + 1, 0x20 + 10 do
			set_t[i] = card_nums[i]
		end
	end
	--不要获取比的情况	
	local chi_data = {}
	local set_t = table.copy(set)
	self:add(set_t,single)
	local all_chi = self:getAllJiaoShun(single,set_t)
	for k,v in pairs(all_chi) do
		local set_tt = table.copy(set_t)
		for i=1,#v do
			self:drop(set_tt,v[i])
		end
		local all_comp = self:getCompare(single,set_tt)
		if not all_comp or not next(all_comp) then
			table.insert(chi_data,{cards = v ,comp = all_comp})
		else
			for i=1,#all_comp do
				table.insert(chi_data,{cards = v ,comp = all_comp[i]})
			end
		end
	end
	print("_____all_chi_",all_chi)
	if #chi_data > 0 then
		print("___chi_data",chi_data)
		return true,chi_data
	else
		return false
	end
	
end

--获取所有的绞和顺 返回所有的绞和顺并返回除这些牌以外的新牌组
function Rule:getAllJiaoShun(card,set)
	local set_t = table.copy(set)
	-- local card_nums = self:getAllValueNums(set_t)
	local jiao = self:getAllJiao(card,set_t)
	local shun = self:getAllShun(card,set_t)

	return self:mergeTable(jiao,shun)
end

function Rule:mergeTable(t1,t2)
	local ret = {}
	for k,v in pairs(t1) do
		table.insert(ret,v)
	end
	for k,v in pairs(t2) do
		table.insert(ret,v)
	end
	return ret
end

function Rule:drop(t,c)
	for i,v in ipairs(t) do
		if v == c then
			table.remove(t,i)
			return true
		end
	end
end

function Rule:add(t,c)
	if type(c) == "table" then
		for _,v in ipairs(c) do
			table.insert(t,v)
		end
	else
		table.insert(t,c)
	end
end

--一样的牌有多少个
function Rule:get_specify_nums(t,c)
	local num = 0
	for i,v in ipairs(t) do
		if v == c then
			num = num + 1
		end
	end
	return num
end

--取出绞牌
function Rule:getAllJiao(card,cards)
	local tempCards = table.copy(cards)
	local card_nums = self:getAllValueNums(tempCards)
	local jiao = {}
	if not card then
		for i=1,10 do
			if card_nums[0x10|i] and card_nums[0x20|i] then
				if card_nums[0x10|i]==1 and card_nums[0x20|i]==2 then
					table.insert(jiao,{0x10|i,0x20|i,0x20|i })

				elseif card_nums[0x10|i]==2 and card_nums[0x20|i]==1 then
					table.insert(jiao,{0x10|i,0x10|i,0x20|i})

				elseif card_nums[0x10|i]==2 and card_nums[0x20|i]==2 then
					table.insert(jiao,{0x10|i,0x10|i,0x20|i})
					table.insert(jiao,{0x10|i,0x20|i,0x20|i})
				end
			end
		end
	else
		local other = self:getOtherTypeCard(card)
		if card_nums[card] and card_nums[other] then
			if card_nums[card]==1 and card_nums[other]==2 then
				table.insert(jiao,{card, other, other})

			elseif card_nums[card]==2 and card_nums[other]==1 then
				table.insert(jiao,{card,card,other})

			elseif card_nums[card]==2 and card_nums[other]==2 then
				table.insert(jiao,{card,card,other})
				table.insert(jiao,{card,other,other})
			end
		end
	end
	return jiao
end

--取出顺牌
function Rule:getAllShun(card,cards)
	local _type  = self:getCardType(card)
	local _value = self:getCardNum(card)
	local tempCards  = table.copy(cards)
	local card_nums = self:getAllValueNums(tempCards)
	local shun = {}
	if card then
		if card_nums[card-2] and card_nums[card-1] and card_nums[card] then
			table.insert(shun,{card-2,card-1,card})
		end
		if card_nums[card-1] and card_nums[card] and card_nums[card+1] then
			table.insert(shun,{card-1,card ,card+1})
		end
		if card_nums[card] and card_nums[card+1] and card_nums[card+2] then
			table.insert(shun,{card,card+1,card+2})
		end
		if _value==2 or _value==7 or _value==10 then
			local cardNum = _type
			if self:has(tempCards,(cardNum+2)) and
				self:has(tempCards,(cardNum+7)) and
				self:has(tempCards,(cardNum+10)) then
				table.insert(shun,{cardNum+2,cardNum+7,cardNum+10})								
			end
		end
	else
		for k,v in pairs(card_nums) do
			if card_nums[k+1] and card_nums[k+2] then
				table.insert(shun,{k,k + 1,k + 2})
			end
			if self:getCardNum(k)==2 then
				if card_nums[k+5] and card_nums[k+8] then
					table.insert(shun,{k,k + 5,k + 8})
				end
			end
		end
	end
	return shun
end

--取比
function Rule:getCompare(card,cards)
	if not self:has(cards,card) then
		return
	end
	local comp = {}
	local all_cards = self:getAllJiaoShun(card,cards)
	for k,v in pairs(all_cards) do
		local set_t = table.copy(cards)
		for i=1,#v do
			self:drop(set_t,v[i])
		end
		if self:has(set_t,card) then
			local all_cards2 = self:getAllJiaoShun(card,set_t)
			if #all_cards2 > 0 then
				for x,y in pairs(all_cards2) do
					table.insert(comp,{{cards=v},{cards=y}})
				end
			else
				print("============")
			end
		else
			table.insert(comp,{{cards=v}})
		end
	end
	return comp
end

--能否偎牌
function Rule:checkWei(cards,card)
	local cardNums = self:getAllValueNums(cards)
	if cardNums[card] and cardNums[card] == 2 then 
		return true
	end
	return false
end

--能否偍牌（跑）
function Rule:checkTi(cards,card)
	local cardNums = self:getAllValueNums(cards)
	if cardNums[card] and cardNums[card] == 3 then 
		return true
	end
	return false
end


--把手牌转为个数表
function Rule:getCardNumTable(cards)
	local tcards = {
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0
	}	
	local card
	local tp 
	for k,v in pairs(cards) do 
		tp = self:getCardType(v)
		card = self:getCardNum(v)
		if tp==0x20 then 
			tcards[card+10]=tcards[card+10]+1
		else
			tcards[card]=tcards[card]+1
		end
	end
	return tcards
end

-- 获取所有能听的牌
function Rule:get_ting_cards(cards,curhuxi,menziNum)
	-- print("____ting___",cards,curhuxi,menziNum)
	local tbRes = {}
	local bHu,huxi,tbMen
	for card=0x11,0x1a do
		bHu,huxi,tbMen = self:checkHu(cards,card,curhuxi,menziNum)
		if bHu then 
			table.insert(tbRes,{bHu,huxi,card,tbMen})
		end
	end
	for card=0x21,0x2a do
		bHu,huxi,tbMen = self:checkHu(cards,card,curhuxi,menziNum)
		if bHu then 
			table.insert(tbRes,{bHu,huxi,card,tbMen})
		end
	end	
	return tbRes
end

-- 判断剩余的手牌是否能胡
function Rule:get_huinfo(cards,huxi,menZi)
	local need_huxi = self.minHuxi - huxi	
	local need_menzi = 7-menZi
	local sum = 0
	local tbShun = {}
	local tbMenZi = {}	
	local tbRes = {}
	local maxHuxi = huxi --最大胡息
	for _,n in ipairs(cards) do
		sum = sum + n
	end
	if sum == 0 then
		--
		print("___11111111____没有手牌了____")
		if need_huxi<=0 and need_menzi==0 then
			return true,maxHuxi,tbRes
		end
		return false,maxHuxi,tbRes
	end
	-- 不需要将的情况
	local need_eye = (sum % 3 == 2)
	-- print(dump(cards))
	-- print("____need_eye__",need_eye,need_huxi)
	for k,v in ipairs(tbl_huxi) do		
		local check = true
		maxHuxi = huxi
		for card,num in pairs(v) do
			if type(card)=="number" and cards[card] < num then
				check = false
				break
			end
		end
		if check then
			--有123或2710 
			-- 扣除相应牌
			for card,num in pairs(v) do
				if type(card) == "number" then
					cards[card] = cards[card] - num
					tbShun = self:splitToMenZi(v)
				end	
			end
			local menzi = need_menzi-#tbShun
			local canhu = self:can_hu(cards,1,need_eye,tbMenZi,menzi)
			-- 加回相应牌
			for card,num in pairs(v) do
				if type(card) == "number" then
					cards[card] = cards[card] + num
				end
			end			
			if canhu then
				if next(tbShun) then 
					-- print("__123，2710__",dump(tbShun))
				end
				-- print("_散门子__",dump(tbMenZi))	
				for _,m in pairs(tbShun) do 
					table.insert(tbRes,m)
				end
				for _,tbm in pairs(tbMenZi) do
					for _,m in pairs(tbm) do 
						table.insert(tbRes,m)
					end
				end
				maxHuxi = maxHuxi + v.huxi
				if self.minHuxi > maxHuxi then
					return false,maxHuxi,tbRes
				end						
				return true,maxHuxi,tbRes
			else
				-- print("__v.huxi____",v.huxi)
				-- maxHuxi = maxHuxi + v.huxi
			end
		end
	end
	if need_huxi > 0 then
		return false,huxi,tbRes
	end
	--没有123，2710 的情况
	if self:can_hu(cards,1,need_eye,tbMenZi,need_menzi) then
		for _,tbm in pairs(tbMenZi) do
			for _,m in pairs(tbm) do 
				table.insert(tbRes,m)
			end
		end		
		-- print("_散门子__",dump(tbMenZi))
		return true,maxHuxi,tbRes
	end
	return false,maxHuxi,tbRes
end


-- 尝试各种拆分方式，只要有一种能胡，则能胡
function Rule:can_hu(cards,i,need_eye,tbMenZi,menZi)
	-- print("_______menZi__",cards,i,need_eye,tbMenZi,menZi)
	if i > 10 then
		print("_______>10")
		return true
	end
	while cards[i] + cards[i+10] == 0 do
		if i == 10 then
			print("_______=10")
			if menZi == 0 then 
				return true
			else
				return false
			end
		end
		i = i + 1
	end

	for sk,split in ipairs(tbl_split) do
		if (not split.eye or need_eye) and split[0]==cards[i] and split[10]==cards[i+10] then
			local can = true
			if (split[2] and (i+2>10 or cards[i+2]<split[2])) or (split[1] and (i+1>10 or cards[i+1]<split[1])) then
				can = false
			end
			
			if (split[12] and (i+12>20 or cards[i+12]<split[12])) or (split[11] and (i+11>20 or cards[i+11]<split[11])) then
				can = false
			end

			if can then
				-- print(dump(split))
				-- print("____i____",i,sk,cards[i],cards[i+10])
				local tbCard = {}		
				local tbLessMenZi = {}
				-- 扣除相关牌
				for k,v in pairs(split) do
					if type(k)=="number" then
						cards[i+k] = cards[i+k] - v
						for a=1,v do 
							table.insert(tbCard,i+k)
						end		
					end
				end
				if next(tbCard) then 
					tbLessMenZi = self:getLessCardMenZi(sk,tbCard)
				end
				table.insert(tbMenZi,tbLessMenZi)	
				local sum = 0
				for _,n in ipairs(cards) do
					sum = sum + n
				end				
				local need_eye = (sum % 3 == 2)	
				local canhu = self:can_hu(cards,i+1,need_eye,tbMenZi,menZi-#tbLessMenZi)
				-- 加回相关牌
				for k,v in pairs(split) do
					if type(k)=="number" then
						cards[i+k] = cards[i+k] + v
					end
				end
				if canhu then
					-- print("tbMenZi____",dump(tbMenZi))	
					local num = 0
					for k,v in pairs(tbMenZi) do 
						num = num + #v
					end
					-- print("___num__",num,menZi)
					return true
				else
					table.remove(tbMenZi,#tbMenZi)
				end
			end
		end
	end
	return false
end

--多个123，2710拆成单个门子
function Rule:splitToMenZi(tb)
	local tbShun = {
		{1,2,3},
		{11,12,13},
		{2,7,10},
		{12,17,20},
	}
	local res = {}
	local has
	local tempList = table.copy(tb)
	for i=1,4 do 
		for _,v in pairs(tbShun) do 
			has = true
			for _,card in pairs(v) do 
				if not tempList[card] or tempList[card]==0 then 
					has = false
				end
			end
			if has then 
				for _,card in pairs(v) do 
					tempList[card] = tempList[card] -1
				end
				-- print(dump(v))
				table.insert(res,v)
			end
		end
	end
	return res
end

function Rule:getDui(tcards)
	local res = {}
	for k,v in pairs(tcards) do
		if v>=2 then
			table.insert(res, {k,k})
		end
	end
	return res
end
--取一个顺子
function Rule:getShun(tcards)
	local res = {}
	local a,b,c
	for k,v in pairs(tcards) do
		b = true
		while b do 
			a = tcards[k+1]
			c = tcards[k+2]
			if v>0 and a and c and a>0 and c>0 then
				tcards[k+1]=tcards[k+1]-1
				tcards[k+2]=tcards[k+2]-1
				tcards[k]=tcards[k]-1
				table.insert(res,{k,k+1,k+2})
			else
				b=false
			end
			if tcards[k]<=0 then 
				b=false
			end
		end
	end
	return res
end
function Rule:getJiao(tcards)
	local res = {}
	local a,b 
	for k,v in pairs(tcards) do		
		a = tcards[k]
		b = tcards[k+10]
		if a and b then 
			if a>=1 and b>= 2 then
				table.insert(res,{k,k+10,k+10})
			elseif a>=2 and b>= 1 then
				table.insert(res,{k,k,k+10})
			elseif a>=2 and b>=2 then
				table.insert(res,{k,k,k+10})
				table.insert(res,{k,k+10,k+10})
			end
		end
	end	
	return res
end

function Rule:getLessCardMenZi(sk,tbCard)
	local tcards = {
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0
	}
	local res = {}

	for k,v in pairs(tbCard) do
		tcards[v] = tcards[v]+1
	end		
	local temp = table.copy(tcards)
	local tbShun = self:getShun(temp)
	temp = table.copy(tcards)
	local tbJiao = self:getJiao(temp)
	temp = table.copy(tcards)
	local tbDui = self:getDui(temp)
	-- print("shun",dump(tbShun))
	-- print("jiao",dump(tbJiao))
	-- print("dui",dump(tbDui))
	-- if 1 then return end
	-- 1顺子 1-2
	if sk>=1 and sk<=2 then 
		return tbShun
	end
	-- 2顺子 3-5
	if sk>=3 and sk<=5 then 
		return tbShun
	end	
	-- 3顺子 6-7
	if sk>=6 and sk<=7 then 
		return tbShun
	end		
	-- 1绞 8-9
	if sk>=8 and sk<=9 then 
		return tbJiao
	end	
	-- 1绞一顺 10-11 12-13
	if sk>=10 and sk<=13 then 
		table.insert(res,tbJiao[1])
		table.insert(res,tbShun[1])
	end		
	-- 1将 14-15
	if sk>=14 and sk<=15 then 
		return tbDui
	end		
	-- 1将1顺 16-19
	if sk>=16 and sk<=19 then 
		table.insert(res,tbDui[1])
		table.insert(res,tbShun[1])
	end		
	-- 1将2顺 20-23 
	if sk>=20 and sk<=23 then 
		local temp = table.copy(tcards)		
		local ts = {}
		for k,v in pairs(temp) do 
			if v>=2 then 				
				temp[k]=temp[k]-2
				ts = self:getShun(temp)
				if #ts == 2 then 
					table.insert(res,{k,k})
					for i,j in pairs(ts) do 
						table.insert(res,j)
					end
					return res
				end		
				temp[k]=temp[k]+2		
			end
		end
	end		
	-- 1将3顺 24-25
	if sk>=24 and sk<=25 then 
		local temp = table.copy(tcards)		
		local ts = {}
		for k,v in pairs(temp) do 
			if v>=2 then 
				temp[k]=temp[k]-2
				ts = self:getShun(temp)
				if #ts == 3 then 
					table.insert(res,{k,k})
					for i,j in pairs(ts) do 
						table.insert(res,j)
					end
					return res
				end		
				temp[k]=temp[k]+2		
			end
		end
	end		
	-- 1将1绞 26-27
	if sk>=26 and sk<=27 then 
		local temp = table.copy(tcards)		
		local ts = {}
		for k,v in pairs(temp) do 
			if v==2 then 
				temp[k]=temp[k]-2
				ts = self:getJiao(temp)
				if #ts == 1 then 
					table.insert(res,{k,k})
					for i,j in pairs(ts) do 
						table.insert(res,j)
					end
					return res
				end		
				temp[k]=temp[k]+2		
			end
		end
	end		
	return res
end


--能否胡牌
--cards 手上的牌
--card 摸的牌
--huxi 已有的胡息
--menziNum 已有的门子
function Rule:checkHu(cards,card,huxi,menziNum)
	--[[
		1.至少15胡息
		2.7方门子
	--]]
	log.debug("___checkHu__",cards,card,huxi,menziNum)
	self:sortCards(cards)
	local cards = self:getCardNumTable(cards)
	if card then 
		local tp = self:getCardType(card)
		card = self:toOtherType(card)		
		cards[card] = cards[card] + 1
	end
	-- huxi = 15
	-- menziNum = 5
	local bHu, huxi,tbMen = self:get_huinfo(cards,huxi,menziNum)
	-- print("胡结果:",bHu,huxi)
	-- print("门子",dump(tbMen))
	local tbMen = table.copy(tbMen) --防止出现重叠引用问题
	if bHu then 
		for _,v in pairs(tbMen) do 
			for k,c in pairs(v) do 
				if c <= 10 then 
					v[k] = 0x10+c
				else
					v[k] = 0x20+(c-10)
				end
			end
		end
	end
	return bHu,huxi,tbMen

end

function Rule:toOtherType(card)
	local tp = self:getCardType(card)
	card = self:getCardNum(card)
	if tp==0x20 then 
		card = card + 10
	end	
	return card
end

--取123，2710胡息
function Rule:getHuxi123(tbCard)
	local sum = 0
	local maxHuxi = 0 --最大胡息
	local cards = self:getCardNumTable(tbCard)
	for _,n in ipairs(cards) do
		sum = sum + n
	end
	if sum == 0 then
		return 0
	end
	for k,v in ipairs(tbl_huxi) do
		-- 检查相应牌
		local check = true
		for card,num in pairs(v) do
			if type(card)=="number" and cards[card] < num then
				check = false
				break
			end
		end
		if check then
			--有123或2710 
			-- 扣除相应牌
			for card,num in pairs(v) do
				if type(card) == "number" then
					cards[card] = cards[card] - num
				end	
			end
			-- print("__v.huxi____",v.huxi)
			if maxHuxi < v.huxi then 
				maxHuxi = v.huxi
			end
		end
	end
	return maxHuxi
end

--名堂
--bankerDeal庄出牌
--ziMo 自摸
--huxi 当前胡息
function Rule:checkMingTang(tbMenzi,bankerDeal,ziMo,huxi,canHaiDi,curRound)
	local tbMingTangHu = {}
	local mingTang = {}
	--1点红
	local redNum = 0	
	local redBianNum = 0
	local redBian = false
	local isRedBain = false
	for _,menzi in pairs(tbMenzi) do 
		redBian = true
		for k,card in pairs(menzi.hand) do 
			if self:getCardColor(card) == self.colorType.red then 
				redNum = redNum + 1
			else
				redBian = false
			end
		end
		if redBian then 
			redBianNum = #menzi.hand
			isRedBain = true
		end
	end
	-- print("___________redNum____",redNum)
	if redNum == 1 then
		--一点红
		mingTang = {name="一点红",fan=1,huxi=0}
		table.insert(tbMingTangHu,mingTang)
	end
	if redNum == 0 then 
		--黑胡
		mingTang = {name="乌胡",fan=0,huxi=100}
		table.insert(tbMingTangHu,mingTang)		
	end
	if redNum >= 10 then 
		--红胡
		if redNum >= 13 then 
			mingTang = {name="红胡",fan=0,huxi=100}
			table.insert(tbMingTangHu,mingTang)	
		elseif redNum >= 10 then 
			mingTang = {name="红胡",fan=(redNum-9),huxi=0}
			table.insert(tbMingTangHu,mingTang)				
		end			
	end	
	if isRedBain and redBianNum == redNum then 
		--一块扁
		mingTang = {name="一块扁",fan=1,huxi=0}
		table.insert(tbMingTangHu,mingTang)	
	end

	local lessCardNum = table.size(self.tbCard)
	if canHaiDi and lessCardNum == 0 then 
		--海底胡
		mingTang = {name="海底胡",fan=1,huxi=0}
		table.insert(tbMingTangHu,mingTang)			
	end

	if lessCardNum == 19 and curRound then 
		--天胡 地胡
		if curRound==2 then
			--庄出牌
			mingTang = {name="地胡",fan=0,huxi=100}
			table.insert(tbMingTangHu,mingTang)				
		elseif curRound==1 then
			mingTang = {name="天胡",fan=0,huxi=100}
			table.insert(tbMingTangHu,mingTang)	
		end
	end
	--卡胡
	if huxi == 30 then 
		mingTang = {name="卡胡",fan=0,huxi=100}
		table.insert(tbMingTangHu,mingTang)	
	end		
	if huxi == 20 then 
		mingTang = {name="卡胡",fan=1,huxi=0}
		table.insert(tbMingTangHu,mingTang)	
	end	
	if ziMo then 
		mingTang = {name="自摸",fan=1,huxi=0}
		table.insert(tbMingTangHu,mingTang)	
	else
		--放炮		
	end

	return tbMingTangHu
end





return Rule